<template>
  <div class="app-container">
    <el-row style="margin-bottom: 0">
      <!-- 查询表单 -->
      <el-col :span="19">
        <el-form :inline="true" size="medium">
          <el-form-item label="讲师">
            <el-autocomplete
              :fetch-suggestions="querySearch"
              :trigger-on-focus="false"
              clearable
              v-model="searchObj.name"
              class="inline-input"
              placeholder="姓名"
              value-key="name"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchObj.level" clearable placeholder="头衔" style="width:150px">
              <el-option value="1" label="初级讲师"/>
              <el-option value="2" label="中级讲师"/>
              <el-option value="3" label="高级讲师"/>
              <el-option value="4" label="首席讲师"/>
            </el-select>
          </el-form-item>
          <el-form-item label="入驻时间">
            <el-date-picker v-model="searchObj.joinDateBegin"
                            placeholder="起始查询时间" value-format="yyyy-MM-dd" style="width:150px"
            />
          </el-form-item>
          <el-form-item label="-">
            <el-date-picker v-model="searchObj.joinDateEnd"
                            placeholder="结束查询时间" value-format="yyyy-MM-dd" style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 工具栏 -->
      <el-col :span="5" style="text-align: right">
        <div style="margin-top: 4px">
          <el-button round type="danger" icon="el-icon-delete" size="small" @click="removeByIds()">批量删除
          </el-button>
          <el-button round type="success" icon="el-icon-download" size="small" @click="output()">导出</el-button>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <el-table
          :data="list"
          :header-cell-style="{'text-align':'center'}"
          fit
          stripe
          tooltip-effect="light"
          v-loading="loading"
          highlight-current-row
          element-lodaing-text="数据加载中"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" min-width="2%" align="center"/>
          <el-table-column type="index" label="#" min-width="5%" align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="avatar" label="头像" min-width="8%" align="center">
            <template slot-scope="scope">
              <el-avatar :size="50" :src="scope.row.avatar" fit="cover" style="margin: 0 auto -6px;"/>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" min-width="8%" sortable align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.name }}
              <a :href="USER_API + '/teacher/' + scope.row.id" target="_blank">
                <i class="el-icon-link"/>
              </a>
            </template>
          </el-table-column>
          <el-table-column :filters="filters" :filter-method="filterHandler" prop="level" label="头衔" min-width="7%"
                           align="center"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.level === 1" type="primary" size="mini" effect="plain">初级讲师</el-tag>
              <el-tag v-if="scope.row.level === 2" type="success" size="mini" effect="plain">中级讲师</el-tag>
              <el-tag v-if="scope.row.level === 3" type="warning" size="mini" effect="plain">高级讲师</el-tag>
              <el-tag v-if="scope.row.level === 4" type="danger" size="mini" effect="plain">首席讲师</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="career" label="资历" min-width="26%" show-overflow-tooltip/>
          <el-table-column prop="style" label="授课风格" min-width="18%"/>
          <el-table-column prop="joinDate" label="入驻时间" sortable min-width="11%" align="center">
            <template slot-scope="scope">
              <i class="el-icon-date"></i>
              {{ scope.row.joinDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="15%" align="center">
            <template slot-scope="scope">
              <el-button-group>
                <el-button type="success" icon="el-icon-edit" size="small" plain
                           @click="edit(scope.row.id)"
                >修改
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" plain
                           @click="removeById(scope.row.id)"
                >删除
                </el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { teacherApi } from '@/api'

export default {
  // 定义数据模型
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 讲师列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      searchObj: {},// 查询条件
      filters: [
        { text: '初级讲师', value: 1 },
        { text: '中级讲师', value: 2 },
        { text: '高级讲师', value: 3 },
        { text: '首席讲师', value: 4 }
      ], // 头衔过滤器
      selection: [], // 选中的行
      USER_API: process.env.VUE_APP_WEB_API
    }
  },
  computed: {},
  // 页面渲染后获取数据
  created() {
    this.fetchData()
  },
  // 定义方法
  methods: {
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      teacherApi.queryByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    removeById(id) {
      this.$confirm('将会删除这条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return teacherApi.deleteById(id)
      }).then(() => {
        this.fetchData()
        this.$message.success('删除成功')
      }).catch(error => {
        // 当取消时会进入catch语句error = 'cancel'
        // 当后端服务抛出异常时error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    // 批量删除
    removeByIds() {
      if (this.selection.length === 0) {
        this.$message.warning('请选择要删除的记录')
        return
      }
      this.$confirm('将会删除这些记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 讲选中行中的id一个一个提取出来
        let idList = []
        this.selection.forEach(item => {
          idList.push(item.id)
        })
        // 调用api
        return teacherApi.deleteByIds(idList)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    edit(id) {
      this.$router.push('/teacher/edit/' + id)
    },
    output() {
      if (this.selection.length === 0) {
        this.$message.warning('请选择要导出的记录')
        return
      }
      this.$confirm('将会导出这些记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(this.selection)
        // 调用api
        return teacherApi.output(this.selection)
      }).then((response) => {
          this.$notify.success({
            title: response.message + ' 文件目录',
            message: response.data.filePath,
            duration: 8000
          })
        }
      ).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消导出')
        }
      })
    },
    // 更改每页记录数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 更改页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 过滤器方法
    filterHandler(value, row) {
      console.log(value)
      return row.level === value
    },
    // 复选框选项发生变化时
    handleSelectionChange(selection) {
      this.selection = selection
    },
    // 弹出输入建议
    querySearch(queryString, cb) {
      teacherApi.queryNamesByKey(queryString).then(response => {
        cb(response.data.nameList)
      })
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";
</style>
